package cn.demomaster.quickui_sample.ui.fragment.helper;

import android.Manifest;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.demomaster.quickcompat.widget.button.QuickButton;

import org.jetbrains.annotations.NotNull;

import cn.demomaster.quicklogger.QdLog;
import cn.demomaster.quickpermission.QuickPermission;
import cn.demomaster.quickpermission.listener.PermissionResultCallback;
import cn.demomaster.quickrouter.fragment.QuickFragment;
import cn.demomaster.quickui.annotation.ActivityPager;
import cn.demomaster.quickui.annotation.ResType;
import cn.demomaster.quickui.util.GPSUtils;
import cn.demomaster.quickui_sample.R;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;


/**
 * Squirrel桓
 * 2018/8/25
 */

@ActivityPager(name = "定位", preViewClass = TextView.class, resType = ResType.Custome)
public class PositionFragment extends QuickFragment {

    QuickButton btn_get_position;
    QuickButton btn_get_position2;

    @NonNull
    @Override
    public View onGenerateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup mView = (ViewGroup) inflater.inflate(R.layout.fragment_layout_position, null);
        return mView;
    }

    public void initView(View rootView) {
        btn_get_position = findViewById(R.id.btn_get_position);
        btn_get_position2 = findViewById(R.id.btn_get_position2);
        getActionBarTool().setTitle("获取定位");
        btn_get_position.setOnClickListener(v -> QuickPermission.with(getActivity()).permissions( PERMISSIONS_POSITION)
                .onGranted(new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        getposition();
                        return null;
                    }
                }).request());

        btn_get_position2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.removeCallbacks(runnable);
                handler.postDelayed(runnable,6000);
            }
        });
    }

    private void getposition() {
        GPSUtils.getInstance(mContext).getLngAndLat(new GPSUtils.LocationResultListener() {

            @Override
            protected void onLocationResult(Location location) {
                QdLog.i("当前位置：" + location.getLatitude() + "," + location.getLongitude());
            }

            @Override
            public void onLocationChanged(@NonNull @NotNull Location location) {
                super.onLocationChanged(location);
                QdLog.i("onLocationChanged：" +location);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                QdLog.i("onStatusChanged：" );
            }

            @Override
            public void onProviderDisabled(@NonNull @NotNull String provider) {
                super.onProviderDisabled(provider);
            }

            @Override
            public void onProviderEnabled(@NonNull @NotNull String provider) {
                super.onProviderEnabled(provider);
            }
        });
    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.removeCallbacks(runnable);
            getposition();
            handler.postDelayed(runnable,3000);
        }
    };

    private static String[] PERMISSIONS_POSITION = {
            Manifest.permission.ACCESS_FINE_LOCATION};

}